﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PhotoList.Web.User
{
    public partial class ucSearch : System.Web.UI.UserControl
    {
        protected string ResultList = "";
        protected string Path = "/";
        public string Keywords = "";
        public bool IsFull = false;
        public bool IsCase = false;
        public bool IsRegular = false;

        protected string PictureDirectory = ConfigurationManager.AppSettings["HomeDirectory"];
        protected string ThumbnailWidth = ConfigurationManager.AppSettings["ThumbnailWidth"];
        protected string ThumbnailHeight = ConfigurationManager.AppSettings["ThumbnailHeight"];
        protected string userHome = ""; //用户主目录
        protected string pathDir = "";  //当前图片目录


        protected void Page_Load(object sender, EventArgs e)
        {
            if (PictureDirectory.EndsWith("/") || PictureDirectory.EndsWith(@"\"))
            {
                PictureDirectory = PictureDirectory.Remove(PictureDirectory.Length - 1);
            }

            if (!GetHome()) return;

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["keywords"])) Search();
            }
        }

        private bool GetHome()
        {
            PhotoList.Dal.User uUser = new PhotoList.Dal.User(Session["QT_UID"].ToString());

            if (uUser.Home == "")
            {
                new QT.Basic.Tools().Alert("未设置用户主目录");
                return false;
            }

            userHome = uUser.Home;

            if (!userHome.StartsWith("/") && !userHome.StartsWith(@"\"))
            {
                userHome = "/" + uUser.Home;
            }

            if (userHome.EndsWith("/") || userHome.EndsWith(@"\"))
            {
                userHome = userHome.Remove(userHome.Length - 1);
            }

            if (!Directory.Exists(Server.MapPath(PictureDirectory + userHome)))
            {
                new QT.Basic.Tools().Alert("用户主目录不存在");
                return false;
            }

            return true;
        }

        protected void Search()
        {
            Path = Request.QueryString["path"];
            Keywords = Request.QueryString["keywords"];
            IsFull = Request.QueryString["isfull"]=="1"?true:false;
            IsCase = Request.QueryString["iscase"] == "1" ? true : false;
            IsRegular = Request.QueryString["isregular"] == "1" ? true : false;
            pathDir = Path;

            //关键字转正则表达式
            string regexp = ".*";
            if (!IsRegular)
            {
                regexp = Keywords;
                regexp = regexp.Replace("+", @"\+");
                regexp = regexp.Replace(".", @"\.");
                regexp = regexp.Replace("(", @"\(");
                regexp = regexp.Replace(")", @"\)");
                regexp = regexp.Replace("[", @"\[");
                regexp = regexp.Replace("]", @"\]");
                regexp = regexp.Replace("{", @"\{");
                regexp = regexp.Replace("}", @"\}");
                regexp = regexp.Replace("?", @".");
                regexp = regexp.Replace("*", @".*");
                if (!regexp.StartsWith("?") && !regexp.StartsWith("*") && !regexp.StartsWith(@".")) regexp = @".*" + regexp;
                if (!regexp.EndsWith("?") && !regexp.EndsWith("*") && !regexp.EndsWith(@".")) regexp += @".*";
            }
            else regexp = Keywords;

            //SearchData
            DataTable dt = PhotoList.Dal.IO.Search(PictureDirectory + userHome + Path, regexp, IsFull, IsCase, IsRegular);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["type"].ToString())
                {
                    case "-1":
                        ResultList += "<div class=\"div\" style=\"text-align:center; \"><a href=\"" + Request.Url.AbsolutePath + "?target=User/ucList&dir=" + pathDir + "/" + dt.Rows[i]["name"].ToString() + "\" title=" + dt.Rows[i]["name"].ToString() + "\" target=\"_self\"><div class=\"divimg\" title=\"" + dt.Rows[i]["name"].ToString() + "\"><img src=\"/img/filetype/Directory.png\" alt=\"" + dt.Rows[i]["name"].ToString() + "\" title=\"" + dt.Rows[i]["name"].ToString() + "\" onload=\"changeImg(this," + ThumbnailWidth + "," + ThumbnailHeight + ")\" /></div><div class=\"divlink\">" + dt.Rows[i]["shortname"].ToString() + "</div></a></div>\n\t";
                        break;
                    case "0":
                        ResultList += "<div class=\"div\" style=\"text-align:center; \"><a href=\"" + PictureDirectory + userHome + pathDir + "/" + dt.Rows[i]["name"].ToString() + "\" title=" + dt.Rows[i]["name"].ToString() + "\" target=\"_blank\"><div class=\"divimg\" title=\"" + dt.Rows[i]["name"].ToString() + "\"><img  alt=\"" + dt.Rows[i]["name"].ToString() + "\" title=\"" + dt.Rows[i]["name"].ToString() + "\" onload=\"changeImg(this," + ThumbnailWidth + "," + ThumbnailHeight + ")\" /></div><div class=\"divlink\">" + dt.Rows[i]["shortname"].ToString() + "</div></a></div>\n\t";
                        break;
                    case "1":
                        ResultList += "<div class=\"div\" style=\"text-align:center; \"><a href=\"" + PictureDirectory + userHome + pathDir + "/" + dt.Rows[i]["name"].ToString() + "\" title=\"" + dt.Rows[i]["name"].ToString() + "\" target=\"_blank\"><div class=\"divimg\" title=\"" + dt.Rows[i]["name"].ToString() + "\"><object><param name=\"movie\" value=\"" + PictureDirectory + "/" + dt.Rows[i]["name"].ToString() + "\" /><param name=\"quality\" value=\"high\" /><param name=\"wmode\" value=\"transparent\" /><embed src=\"" + PictureDirectory + "/" + dt.Rows[i]["name"].ToString() + "\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + ThumbnailWidth + "\" height=\"" + ThumbnailHeight + "\"></embed></object></div><div class=\"divlink\">" + dt.Rows[i]["shortname"].ToString() + "</div></a></div>\n\t";
                        break;
                    default:
                        string icoFile = "/img/filetype/" + dt.Rows[i]["ext"].ToString() + ".png";
                        if (!File.Exists(Server.MapPath(icoFile))) icoFile = "/img/filetype/UnKnown.png";
                        ResultList += "<div class=\"div\" style=\"text-align:center; \"><a href=\"" + PictureDirectory + userHome + pathDir + "/" + dt.Rows[i]["name"].ToString() + "\" title=\"" + dt.Rows[i]["name"].ToString() + "\" target=\"_blank\"><div class=\"divimg\" title=\"" + dt.Rows[i]["name"].ToString() + "\"><img src=\"" + icoFile + "\" class=\"img\" title=\"" + dt.Rows[i]["name"].ToString() + "\" onload=\"changeImg(this," + ThumbnailWidth + "," + ThumbnailHeight + ")\" /></div><div class=\"divlink\">" + dt.Rows[i]["shortname"].ToString() + "</div></a></div>\n\t";
                        break;
                }
            }
        }
    }
}